﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using AntsCode.DevStorageSyncLibrary;

namespace DevStorageSync
{
    public partial class SyncForm : Form
    {
        private Synchroniser sync;

        public SyncForm(string[] assemblies)
        {
            InitializeComponent();

            // Intialise the synchroniser
            sync = new Synchroniser(assemblies);

            // Udpate the entity set list
            UpdateEntitySets(sync.EntitySets);
        }

        private void UpdateEntitySets(IEnumerable<DataServiceEntitySet> entitySets)
        {
            foreach (DataServiceEntitySet entitySet in entitySets)
            {
                entitySetList.Items.Add(entitySet.Name);
            }
        }

        private void Sync_Click(object sender, EventArgs e)
        {
            if (SyncAll.Checked)
            {
                Sync.Text = "Please wait...";
                Sync.Enabled = false;
                Cancel.Enabled = false;

                sync.SyncAllEntitySets();
            }
            else
            {
                if (entitySetList.SelectedIndex >= 0)
                {
                    Sync.Text = "Please wait...";
                    Sync.Enabled = false;
                    Cancel.Enabled = false;

                    sync.SyncEntitySet(entitySetList.SelectedItem.ToString());
                }
                else
                {
                    MessageBox.Show("Please select a table.");
                    return;
                }
            }

            MessageBox.Show("Synchronisation Successful.");
            this.Close();
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void entitySetList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SyncSelected.Checked = true;
        }
    }
}